/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.desk.entity.Alerts;
import org.springblade.modules.desk.mapper.AlertsMapper;
import org.springblade.modules.monitor.entity.Application;
import org.springblade.modules.monitor.mapper.ApplicationMapper;
import org.springblade.modules.monitor.service.IApplicationService;
import org.springblade.modules.monitor.vo.ApplicationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static org.springblade.common.tool.CommonUtil.getDefaultAlert;
import static org.springblade.common.tool.CommonUtil.getSecondFromTime;

/**
 *  服务实现类
 *
 * @author Blade
 * @since 2020-08-17
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements IApplicationService {
	@Autowired
	private ApplicationMapper applicationMapper;
	@Autowired
	private AlertsMapper alertsMapper;

	@Override
	public IPage<ApplicationVO> selectApplicationPage(IPage<ApplicationVO> page, ApplicationVO application) {
		return page.setRecords(baseMapper.selectApplicationPage(page, application));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchUpdateStatus(List<Application> applications,List<Application> preApplications){
		applications.forEach(x->{
			Application temp = new Application();
			temp.setId(x.getId());
			temp.setStatus(x.getStatus());
			applicationMapper.updateById(temp);
		});

		LocalDateTime localDateTime = LocalDateTime.now();
		String createTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"));
		applications.forEach(x->{
			preApplications.forEach(y->{
				if(x.getId().equals(y.getId())){
					//之前状态正常，现在状态存在问题
					if(y.getStatus().equals(1)&&x.getStatus().equals(0)){
						Alerts defaultAlert = getDefaultAlert();
						defaultAlert.setSubject("Problem: 应用无法访问");
						defaultAlert.setMessage("hostName:::"+y.getName()+" |||\n" +
							"hostIP:::"+ y.getIp() +" |||\n" +
							"eventSeverity:::4 |||\n" +
							"eventName:::"+y.getName()+"应用无法访问 |||\n" +
							"eventTime:::"+createTime+" |||");
						alertsMapper.insert(defaultAlert);
					}
				}
			});
		});
	}

}
